{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# [RAILSGUIDES](https://railsguides.jp/)\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Railsのインストール"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Writing new Gemfile to /root/python/jupyter_notebook/Rails/RAILSGUIDES/Gemfile\n",
      "\n",
      "# frozen_string_literal: true\n",
      "source \"https://rubygems.org\"\n",
      "\n",
      "gem \"rails\"\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### Gemfile作成\n",
    "puts `bundle init`\n",
    "\n",
    "### railsのコメントアウトを外す\n",
    "buffer = File.open(\"Gemfile\", \"r\") { |f| f.read() }\n",
    "buffer.gsub!('# gem \"rails\"', 'gem \"rails\"')\n",
    "File.open(\"Gemfile\", \"w\") { |f| f.write(buffer) }\n",
    "File.open(\"Gemfile\",\"r\") {|f| puts f.read}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't run Bundler as root. Bundler can ask for sudo if it is needed, and\n",
      "installing your bundle as root will break this application for all non-root\n",
      "users on this machine.\n",
      "Fetching gem metadata from https://rubygems.org/.........\n",
      "Fetching version metadata from https://rubygems.org/..\n",
      "Fetching dependency metadata from https://rubygems.org/.\n",
      "Installing rake 12.0.0\n",
      "Installing concurrent-ruby 1.0.5\n",
      "Installing i18n 0.8.6\n",
      "Installing minitest 5.10.2\n",
      "Installing thread_safe 0.3.6\n",
      "Installing builder 3.2.3\n",
      "Installing erubi 1.6.1\n",
      "Installing mini_portile2 2.2.0\n",
      "Installing rack 2.0.3\n",
      "Installing nio4r 2.1.0 with native extensions\n",
      "Installing websocket-extensions 0.1.2\n",
      "Installing mime-types-data 3.2016.0521\n",
      "Installing arel 8.0.0\n",
      "Using method_source 0.8.2\n",
      "Using bundler 1.14.6\n",
      "Installing thor 0.19.4\n",
      "Installing tzinfo 1.2.3\n",
      "Installing nokogiri 1.8.0 with native extensions\n",
      "Installing rack-test 0.6.3\n",
      "Installing sprockets 3.7.1\n",
      "Installing websocket-driver 0.6.5 with native extensions\n",
      "Installing mime-types 3.1\n",
      "Installing activesupport 5.1.2\n",
      "Installing loofah 2.0.3\n",
      "Installing mail 2.6.6\n",
      "Installing rails-dom-testing 2.0.3\n",
      "Installing globalid 0.4.0\n",
      "Installing activemodel 5.1.2\n",
      "Installing rails-html-sanitizer 1.0.3\n",
      "Installing activejob 5.1.2\n",
      "Installing activerecord 5.1.2\n",
      "Installing actionview 5.1.2\n",
      "Installing actionpack 5.1.2\n",
      "Installing actioncable 5.1.2\n",
      "Installing actionmailer 5.1.2\n",
      "Installing railties 5.1.2\n",
      "Installing sprockets-rails 3.2.0\n",
      "Installing rails 5.1.2\n",
      "Bundle complete! 1 Gemfile dependency, 38 gems now installed.\n",
      "Bundled gems are installed into /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### bundle installの実行。upyter上からインストールする場合はFullPATHで指定\n",
    "### 「Could not find gem 'rails' in any of the gem sources listed in your Gemfile. (Bundler::GemNotFound)」\n",
    "### で失敗するときは一度コンソール上で実行してからJupyter上で実行すると成功する\n",
    "### \n",
    "#puts `bundle install --gemfile /Users/ftakao2007/jupyter/jupyter_notebook/Rails/RAILSGUIDES/Gemfile --path /Users/ftakao2007/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle`\n",
    "puts `bundle install --gemfile /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/Gemfile --path /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       exist  \n",
      "      create  README.md\n",
      "      create  Rakefile\n",
      "      create  config.ru\n",
      "      create  .gitignore\n",
      "       force  Gemfile\n",
      "         run  git init from \".\"\n",
      "Initialized empty Git repository in /root/python/jupyter_notebook/Rails/RAILSGUIDES/.git/\n",
      "      create  app\n",
      "      create  app/assets/config/manifest.js\n",
      "      create  app/assets/javascripts/application.js\n",
      "      create  app/assets/javascripts/cable.js\n",
      "      create  app/assets/stylesheets/application.css\n",
      "      create  app/channels/application_cable/channel.rb\n",
      "      create  app/channels/application_cable/connection.rb\n",
      "      create  app/controllers/application_controller.rb\n",
      "      create  app/helpers/application_helper.rb\n",
      "      create  app/jobs/application_job.rb\n",
      "      create  app/mailers/application_mailer.rb\n",
      "      create  app/models/application_record.rb\n",
      "      create  app/views/layouts/application.html.erb\n",
      "      create  app/views/layouts/mailer.html.erb\n",
      "      create  app/views/layouts/mailer.text.erb\n",
      "      create  app/assets/images/.keep\n",
      "      create  app/assets/javascripts/channels\n",
      "      create  app/assets/javascripts/channels/.keep\n",
      "      create  app/controllers/concerns/.keep\n",
      "      create  app/models/concerns/.keep\n",
      "      create  bin\n",
      "      create  bin/bundle\n",
      "      create  bin/rails\n",
      "      create  bin/rake\n",
      "      create  bin/setup\n",
      "      create  bin/update\n",
      "      create  bin/yarn\n",
      "      create  config\n",
      "      create  config/routes.rb\n",
      "      create  config/application.rb\n",
      "      create  config/environment.rb\n",
      "      create  config/secrets.yml\n",
      "      create  config/cable.yml\n",
      "      create  config/puma.rb\n",
      "      create  config/spring.rb\n",
      "      create  config/environments\n",
      "      create  config/environments/development.rb\n",
      "      create  config/environments/production.rb\n",
      "      create  config/environments/test.rb\n",
      "      create  config/initializers\n",
      "      create  config/initializers/application_controller_renderer.rb\n",
      "      create  config/initializers/assets.rb\n",
      "      create  config/initializers/backtrace_silencers.rb\n",
      "      create  config/initializers/cookies_serializer.rb\n",
      "      create  config/initializers/cors.rb\n",
      "      create  config/initializers/filter_parameter_logging.rb\n",
      "      create  config/initializers/inflections.rb\n",
      "      create  config/initializers/mime_types.rb\n",
      "      create  config/initializers/new_framework_defaults_5_1.rb\n",
      "      create  config/initializers/wrap_parameters.rb\n",
      "      create  config/locales\n",
      "      create  config/locales/en.yml\n",
      "      create  config/boot.rb\n",
      "      create  config/database.yml\n",
      "      create  db\n",
      "      create  db/seeds.rb\n",
      "      create  lib\n",
      "      create  lib/tasks\n",
      "      create  lib/tasks/.keep\n",
      "      create  lib/assets\n",
      "      create  lib/assets/.keep\n",
      "      create  log\n",
      "      create  log/.keep\n",
      "      create  public\n",
      "      create  public/404.html\n",
      "      create  public/422.html\n",
      "      create  public/500.html\n",
      "      create  public/apple-touch-icon-precomposed.png\n",
      "      create  public/apple-touch-icon.png\n",
      "      create  public/favicon.ico\n",
      "      create  public/robots.txt\n",
      "      create  test/fixtures\n",
      "      create  test/fixtures/.keep\n",
      "      create  test/fixtures/files\n",
      "      create  test/fixtures/files/.keep\n",
      "      create  test/controllers\n",
      "      create  test/controllers/.keep\n",
      "      create  test/mailers\n",
      "      create  test/mailers/.keep\n",
      "      create  test/models\n",
      "      create  test/models/.keep\n",
      "      create  test/helpers\n",
      "      create  test/helpers/.keep\n",
      "      create  test/integration\n",
      "      create  test/integration/.keep\n",
      "      create  test/test_helper.rb\n",
      "      create  test/system\n",
      "      create  test/system/.keep\n",
      "      create  test/application_system_test_case.rb\n",
      "      create  tmp\n",
      "      create  tmp/.keep\n",
      "      create  tmp/cache\n",
      "      create  tmp/cache/assets\n",
      "       exist  vendor\n",
      "      create  vendor/.keep\n",
      "      create  package.json\n",
      "      remove  config/initializers/cors.rb\n",
      "      remove  config/initializers/new_framework_defaults_5_1.rb\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### rails newの実行\n",
    "puts `bundle exec rails new . -f --skip-bundle`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "source 'https://rubygems.org'\n",
      "\n",
      "git_source(:github) do |repo_name|\n",
      "  repo_name = \"#{repo_name}/#{repo_name}\" unless repo_name.include?(\"/\")\n",
      "  \"https://github.com/#{repo_name}.git\"\n",
      "end\n",
      "\n",
      "\n",
      "# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'\n",
      "gem 'rails', '~> 5.1.2'\n",
      "# Use sqlite3 as the database for Active Record\n",
      "gem 'sqlite3'\n",
      "# Use Puma as the app server\n",
      "gem 'puma', '~> 3.7'\n",
      "# Use SCSS for stylesheets\n",
      "gem 'sass-rails', '~> 5.0'\n",
      "# Use Uglifier as compressor for JavaScript assets\n",
      "gem 'uglifier', '>= 1.3.0'\n",
      "# See https://github.com/rails/execjs#readme for more supported runtimes\n",
      "gem 'therubyracer', platforms: :ruby\n",
      "\n",
      "# Use CoffeeScript for .coffee assets and views\n",
      "gem 'coffee-rails', '~> 4.2'\n",
      "# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks\n",
      "gem 'turbolinks', '~> 5'\n",
      "# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder\n",
      "gem 'jbuilder', '~> 2.5'\n",
      "# Use Redis adapter to run Action Cable in production\n",
      "# gem 'redis', '~> 3.0'\n",
      "# Use ActiveModel has_secure_password\n",
      "# gem 'bcrypt', '~> 3.1.7'\n",
      "\n",
      "# Use Capistrano for deployment\n",
      "# gem 'capistrano-rails', group: :development\n",
      "\n",
      "group :development, :test do\n",
      "  # Call 'byebug' anywhere in the code to stop execution and get a debugger console\n",
      "  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]\n",
      "  # Adds support for Capybara system testing and selenium driver\n",
      "  gem 'capybara', '~> 2.13'\n",
      "  gem 'selenium-webdriver'\n",
      "end\n",
      "\n",
      "group :development do\n",
      "  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.\n",
      "  gem 'web-console', '>= 3.3.0'\n",
      "  gem 'listen', '>= 3.0.5', '< 3.2'\n",
      "  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring\n",
      "  gem 'spring'\n",
      "  gem 'spring-watcher-listen', '~> 2.0.0'\n",
      "end\n",
      "\n",
      "# Windows does not include zoneinfo files, so bundle the tzinfo-data gem\n",
      "gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### therubyracerのコメントアウトを外す\n",
    "buffer = File.open(\"Gemfile\", \"r\") { |f| f.read() }\n",
    "buffer.gsub!(\"# gem 'therubyracer'\", \"gem 'therubyracer'\")\n",
    "File.open(\"Gemfile\", \"w\") { |f| f.write(buffer) }\n",
    "File.open(\"Gemfile\",\"r\") {|f| puts f.read}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't run Bundler as root. Bundler can ask for sudo if it is needed, and\n",
      "installing your bundle as root will break this application for all non-root\n",
      "users on this machine.\n",
      "Fetching gem metadata from https://rubygems.org/.........\n",
      "Fetching version metadata from https://rubygems.org/..\n",
      "Fetching dependency metadata from https://rubygems.org/.\n",
      "Using rake 12.0.0\n",
      "Using concurrent-ruby 1.0.5\n",
      "Using i18n 0.8.6\n",
      "Using minitest 5.10.2\n",
      "Using thread_safe 0.3.6\n",
      "Using builder 3.2.3\n",
      "Using erubi 1.6.1\n",
      "Using mini_portile2 2.2.0\n",
      "Using rack 2.0.3\n",
      "Using nio4r 2.1.0\n",
      "Using websocket-extensions 0.1.2\n",
      "Using mime-types-data 3.2016.0521\n",
      "Using arel 8.0.0\n",
      "Installing public_suffix 2.0.5\n",
      "Installing bindex 0.5.0 with native extensions\n",
      "Installing byebug 9.0.6 with native extensions\n",
      "Installing ffi 1.9.18 with native extensions\n",
      "Installing coffee-script-source 1.12.2\n",
      "Installing execjs 2.7.0\n",
      "Using method_source 0.8.2\n",
      "Using thor 0.19.4\n",
      "Using multi_json 1.12.1\n",
      "Installing libv8 3.16.14.19 (x86_64-linux)\n",
      "Installing rb-fsevent 0.10.2\n",
      "Installing ruby_dep 1.5.0\n",
      "Installing puma 3.9.1 with native extensions\n",
      "Using bundler 1.14.6\n",
      "Installing ref 2.0.0\n",
      "Installing rubyzip 1.2.1\n",
      "Installing tilt 2.0.7\n",
      "Installing sqlite3 1.3.13 with native extensions\n",
      "Installing turbolinks-source 5.0.3\n",
      "Using tzinfo 1.2.3\n",
      "Using nokogiri 1.8.0\n",
      "Using rack-test 0.6.3\n",
      "Using sprockets 3.7.1\n",
      "Using websocket-driver 0.6.5\n",
      "Using mime-types 3.1\n",
      "Installing addressable 2.5.1\n",
      "Installing childprocess 0.7.1\n",
      "Installing rb-inotify 0.9.10\n",
      "Installing coffee-script 2.4.1\n",
      "Installing uglifier 3.2.0\n",
      "Installing therubyracer 0.12.3 with native extensions\n",
      "Installing turbolinks 5.0.1\n",
      "Using activesupport 5.1.2\n",
      "Using loofah 2.0.3\n",
      "Installing xpath 2.1.0\n",
      "Using mail 2.6.6\n",
      "Installing selenium-webdriver 3.4.3\n",
      "Installing listen 3.1.5\n",
      "Installing sass-listen 3.0.7\n",
      "Using rails-dom-testing 2.0.3\n",
      "Using globalid 0.4.0\n",
      "Using activemodel 5.1.2\n",
      "Installing jbuilder 2.7.0\n",
      "Installing spring 2.0.2\n",
      "Using rails-html-sanitizer 1.0.3\n",
      "Installing capybara 2.14.4\n",
      "Installing sass 3.5.0\n",
      "Using activejob 5.1.2\n",
      "Using activerecord 5.1.2\n",
      "Installing spring-watcher-listen 2.0.1\n",
      "Using actionview 5.1.2\n",
      "Using actionpack 5.1.2\n",
      "Using actioncable 5.1.2\n",
      "Using actionmailer 5.1.2\n",
      "Using railties 5.1.2\n",
      "Using sprockets-rails 3.2.0\n",
      "Installing coffee-rails 4.2.2\n",
      "Installing web-console 3.5.1\n",
      "Using rails 5.1.2\n",
      "Installing sass-rails 5.0.6\n",
      "Bundle complete! 17 Gemfile dependencies, 73 gems now installed.\n",
      "Bundled gems are installed into /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle.\n",
      "\n"
     ]
    }
   ],
   "source": [
    "### もう一度bundle install\n",
    "### 失敗するときは一度コンソール上で実行してからJupyter上で実行すると成功することが有る\n",
    "#puts `bundle install --gemfile /Users/ftakao2007/jupyter/jupyter_notebook/Rails/RAILSGUIDES/Gemfile --path /Users/ftakao2007/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle`\n",
    "puts `bundle install --gemfile /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/Gemfile --path /root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle`"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "false"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### 以下ペンディング\n",
    "#l_paths = $LOAD_PATH\n",
    "# r_path = \"/root/jupyter/jupyter_notebook/Rails/RAILSGUIDES/vendor/bundle/ruby/2.3.0/gems/railties-5.1.2/lib/rails\"\n",
    "# $LOAD_PATH << r_path unless l_paths.include?(r_path)\n",
    "\n",
    "# system(\"bundle exec rails generate controller welcome index > /tmp/rails.log\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 作業メモ\n",
    "\n",
    "```\n",
    "# bundle exec rails generate controller welcome index\n",
    "================================================\n",
    "      create  app/controllers/welcome_controller.rb\n",
    "       route  get 'welcome/index'\n",
    "      invoke  erb\n",
    "      create    app/views/welcome\n",
    "      create    app/views/welcome/index.html.erb\n",
    "      invoke  test_unit\n",
    "      create    test/controllers/welcome_controller_test.rb\n",
    "      invoke  helper\n",
    "      create    app/helpers/welcome_helper.rb\n",
    "      invoke    test_unit\n",
    "      invoke  assets\n",
    "      invoke    coffee\n",
    "      create      app/assets/javascripts/welcome.coffee\n",
    "      invoke    scss\n",
    "      create      app/assets/stylesheets/welcome.scss\n",
    "================================================\n",
    "\n",
    "# head config/routes.rb\n",
    "================================================\n",
    "Rails.application.routes.draw do\n",
    "  get 'welcome/index'\n",
    "\n",
    "  # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html\n",
    "end\n",
    "================================================\n",
    "\n",
    "# bundle exec rake routes\n",
    "================================================\n",
    "       Prefix Verb URI Pattern              Controller#Action\n",
    "welcome_index GET  /welcome/index(.:format) welcome#index\n",
    "================================================\n",
    "\n",
    "# cat app/controllers/welcome_controller.rb\n",
    "================================================\n",
    "class WelcomeController < ApplicationController\n",
    "  def index\n",
    "  end\n",
    "end\n",
    "================================================\n",
    "\n",
    "# cat app/views/welcome/index.html.erb\n",
    "================================================\n",
    "<h1>Welcome#index</h1>\n",
    "<p>Find me in app/views/welcome/index.html.erb</p>\n",
    "================================================\n",
    "\n",
    "# bundle exec rails s -b 172.17.0.3\n",
    "================================================\n",
    "=> Booting Puma\n",
    "=> Rails 5.1.2 application starting in development on http://172.17.0.3:3000\n",
    "=> Run `rails server -h` for more startup options\n",
    "Puma starting in single mode...\n",
    "* Version 3.9.1 (ruby 2.3.3-p222), codename: Private Caller\n",
    "* Min threads: 5, max threads: 5\n",
    "* Environment: development\n",
    "* Listening on tcp://172.17.0.3:3000\n",
    "Use Ctrl-C to stop\n",
    "================================================\n",
    "\n",
    "# ブラウザでアクセス\n",
    "http://10.136.3.180:3000/welcome/index\n",
    "```\n",
    "\n",
    "<img src=\"img/rails01.png\" alt=\"rails01\" title=\"rails01\" align=\"left\" />  \n",
    "\n",
    "<br clear=\"left\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## デバッグコンソールのパラメータ\n",
    "\n",
    "一番上 : welcomeコントローラのindexアクション\n",
    "\n",
    "* self\n",
    "    * @_parameter_encodings\n",
    "        * 空のハッシュ\n",
    "    * @abstract\n",
    "        * false\n",
    "    * @action_methods\n",
    "        * nil\n",
    "    * @controller_path\n",
    "        * \"welcome\"\n",
    "            * bytes (配列で要素ごとに1文字「w」「e」...のアスキーコード)\n",
    "            * encoding = US-ASCII\n",
    "    * @parent_name\n",
    "        * nil\n",
    "    * @renderer\n",
    "        *  #<ActionController::Renderer:0x007f50786158a8>\n",
    "        * @controller\n",
    "            * WelcomeController\n",
    "            * (その他パラメータ)\n",
    "        * @defaults\n",
    "            * (keyがシンボルのハッシュ)\n",
    "            * :http_host\n",
    "                * \"example.org\"\n",
    "                * encoding\n",
    "                    * UTF-8\n",
    "            * :https\n",
    "                * false\n",
    "            * :method\n",
    "                * \"get\"\n",
    "            * :script_name\n",
    "                * (空)\n",
    "            * :input\n",
    "                * (空)\n",
    "        * @env\n",
    "            * (defaultsと似たような値)\n",
    "    * params\n",
    "        * (無いのでNameErrorとなっている)\n",
    "    * Global variables\n",
    "        * (グローバル変数)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.4.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.4.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
